package com.cxg.fairy.ec.sign;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TextInputEditText;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.cxg.fairy.delegates.FairyDelegate;
import com.cxg.fairy.ec.R;
import com.cxg.fairy.ec.R2;
import com.cxg.fairy.net.RestClient;
import com.cxg.fairy.net.callback.ISuccess;
import com.cxg.fairy.util.log.FairyLogger;
import com.cxg.fairy.wechat.FairyWeChat;
import com.cxg.fairy.wechat.callbacks.IWeChatSignInCallback;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/11/6.
 */

public class SignInDelegate extends FairyDelegate {

    @BindView(R2.id.edit_login_account)
    TextInputEditText lAccount = null;

    @OnClick(R2.id.btn_login)
    void onClickLogin() {
        if (checkFrom()) {
            if (checkFrom()) {
                RestClient.builder()
                        .url("http://api.gaoqingyy.com/index.php/api/example/users")
                        .success(new ISuccess() {
                            @Override
                            public void onSuccess(String response) {
                                Log.e("HAHHAHAAHHAHAHA", response);
                                FairyLogger.e("ResponseInfo:", response);
                                SignHandler.onSignIn(response, mISignListener);
                            }
                        })
                        .build()
                        .get();
            }
        }
    }

    @OnClick(R2.id.icon_sign_in_wechat)
    void onClickWechat() {
        FairyWeChat.getInstance().onSignSuccess(new IWeChatSignInCallback() {
            @Override
            public void onSignInSuccess(String userInfo) {

            }
        }).signIn();
    }

    private ISignListener mISignListener = null;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof ISignListener) {
            mISignListener = (ISignListener) activity;
        }
    }

    private boolean checkFrom() {
        final String account = lAccount.getText().toString();

        boolean isPass = true;

        if (account.isEmpty()) {
            lAccount.setError("请输入帐号111");
            isPass = false;
        } else {
            lAccount.setError(null);
        }


        return isPass;
    }

    @Override
    public Object setLayout() {
        return R.layout.delegate_sign_in;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {

    }
}
